package servlet;

import dao.impI.FruitDaoImpI;
import service.ImpI.FruitServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/deleteProductServlet",loadOnStartup = 1,
    initParams = {
        @WebInitParam(name="encoding",value="utf-8")
    }
)
public class DeleteProductServlet extends HttpServlet {
    String encoding=null;
    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding=config.getInitParameter("encoding");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        String id = req.getParameter("id");
        System.out.println("待删除产品的id是:"+id);
        //FruitDaoImpI fruitDaoImpI = new FruitDaoImpI();
        //boolean falg=fruitDaoImpI.deleteProductById(Integer.parseInt(id));
        FruitServiceImpI fruitServiceImpI = new FruitServiceImpI();
        boolean flag = fruitServiceImpI.deleteProductById(Integer.parseInt(id));
        System.out.println(flag);
        if (flag){
            //删除成功，则转发到这个servlet去重新获取数据库信息在页面展示
            req.getRequestDispatcher("/productListServlet").forward(req,resp);
        }else {
            req.getRequestDispatcher("/exception.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }




}
